package com.smartrobot.smartrobot.adapter;

import android.content.Context;
import android.content.Intent;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AbsListView;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.nostra13.universalimageloader.core.ImageLoader;
import com.smartrobot.smartrobot.Common.Common;
import com.smartrobot.smartrobot.R;
import com.smartrobot.smartrobot.activity.NewsDetailActivity;
import com.smartrobot.smartrobot.activity.StoreActiveDetailActivity;
import com.smartrobot.smartrobot.logToSdCard.LogToSd;
import com.smartrobot.smartrobot.util.FileUtil;
import com.smartrobot.smartrobot.util.ImageLoaderApi;
import com.smartrobot.smartrobot.util.News;
import com.smartrobot.smartrobot.util.StoreActive;
import com.smartrobot.smartrobot.view.ExListView;
import com.smartrobot.smartrobot.view.MarqueeTextView;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2014/12/23.
 */
public class StoreActiveAdapter extends BaseAdapter implements AdapterView.OnItemClickListener {


    private Context context;
    private List<StoreActive> list;
    private ViewHolder holder;


    public StoreActiveAdapter(Context context){

        this.context = context;
        ImageLoaderApi.initImageLoaderApiInformationPic(context);
        list = new ArrayList<StoreActive>();
//        list.add(new StoreActive("1","沙宣东圃分店","十元优惠大众洗剪吹套装，瞬间变成高富帅，白富美，心动不如行动","2015-1-20"));
//        list.add(new StoreActive("2","天河沙宣分店","过年大优惠，吐血减价","2015-1-20"));
//        list.add(new StoreActive("3","广州大学城沙宣分店","老板不在，偷偷降价大促销，心动不如行动","2015-1-20"));
//        list.add(new StoreActive("4","上海外滩沙宣分店","老板跑路，日用品，统统5折减价大促销","2015-1-20"));
//        list.add(new StoreActive("5","佛山沙宣中国区分店","全场颈减5折，走过路过不要错过","2015-1-20"));
//        list.add(new StoreActive("6","年年有余分店","春节大错小，全场一律一折","2015-1-20"));
//        list.add(new StoreActive("7","恭喜发财中国区分店","不打折","2015-1-20"));
//        list.add(new StoreActive("7","恭喜发财中国区分店","不打折","2015-1-20"));
//        list.add(new StoreActive("7","恭喜发财中国区分店","不打折","2015-1-20"));
//        list.add(new StoreActive("7","恭喜发财中国区分店","不打折","2015-1-20"));



    }

    @Override
    public int getCount() {
        return list.size();
    }

    @Override
    public Object getItem(int i) {
        return i;
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup viewGroup) {

        if(convertView == null){
            //新建一个holder
            convertView =  LayoutInflater.from(context).inflate(R.layout.page_store_active_adapter_item, viewGroup, false);
            holder = new ViewHolder();

            holder.layout = (LinearLayout)convertView.findViewById(R.id.store_adapter_item_layout);
            holder.time = (MarqueeTextView)convertView.findViewById(R.id.store_adapter_item_time);
            holder.title = (MarqueeTextView) convertView.findViewById(R.id.store_adapter_item_title);
            holder.simpleContent = (MarqueeTextView)convertView.findViewById(R.id.store_adapter_item_content);
            holder.hotPic = (ImageView)convertView.findViewById(R.id.store_adapter_item_hot_pic);

            convertView.setTag(holder);

        }else{
            holder = (ViewHolder)convertView.getTag();
        }


        //初始化所有view
        final StoreActive storeActive = list.get(position);
        StringBuilder builder = new StringBuilder();
        builder.append(storeActive.getTime().split(" ")[0]);
        builder.append("--");
        builder.append(storeActive.getValidity().split(" ")[0]);

        Log.d("toString ------------->",storeActive.toString());

        holder.title.setText(storeActive.getTitle());
        holder.simpleContent.setText(storeActive.getSynopsis());
        holder.time.setText(builder.toString());


//        holder.layout.setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View view) {

//            }
//        });


        return convertView;
    }

    @Override
    public void onItemClick(AdapterView<?> adapterView, View view, int position, long l) {

                int pos = position - 1;
                Intent intent = new Intent();
                intent.setClass(context, StoreActiveDetailActivity.class);
                intent.putExtra("title",list.get(pos).getTitle());
                intent.putExtra("url",list.get(pos).getUrl());
                intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                context.startActivity(intent);
    }



    class ViewHolder{

        LinearLayout layout;
        MarqueeTextView title,simpleContent,time;
        ImageView hotPic;


    }

    public void notiNewDataChange(List<StoreActive> list){

        if(list != null)
        if(list.size() > 10){//若是返回动态多余10条就改变动态 这里获取回来绝对是没有的，因为是根据时间排序的
            //引用会改变内部值
            this.list.clear();
            this.list.addAll(list);
            notifyDataSetChanged();
        }else{//若是少于十条就添加到新的里面去
            list.addAll(this.list);
            this.list.clear();
            this.list.addAll(list);
            notifyDataSetChanged();
        }


    }

    public void notiOldDataChange(List<StoreActive> list){

        //通知改变数据
        this.list.addAll(list);
        notifyDataSetChanged();

    }
//
    public String getNewUpdateTime(){
        if(list != null && list.size() > 0){
            return list.get(0).getTimeSign();//返回
        }else
            return "1";
    }

    public String getOldUpdateTime(){
        if(list != null && list.size() >0){
            return list.get(list.size() - 1).getTimeSign();
        }else
            return "1";//这个是最小时间
    }
//
//
    public void saveCircleToFile(String fileName){
        //只是保存十条数据到文件缓存中
        List<StoreActive> saveList ;
        if(this.list.size() > 10)
            saveList = this.list.subList(0,10);
        else
            saveList = this.list;

        try {
            if(FileUtil.createFile(fileName))
            {
                FileUtil.writeStoreActiveToFile(saveList, fileName);
                Log.d("--------->", "保存成功");
                LogToSd.debug("资讯保存文件,保存成功!");
            }
            else
            {
                Log.e("--------->","无法创建文件");
                LogToSd.error("资讯保存文件,错误"+"无法创建文件夹");
            }
        } catch (Exception e) {
            Log.e("--------->","无法找到文件");
            LogToSd.error("资讯保存文件,错误："+e.getMessage().toString());
        }
    }


}
